<!-- 公用的用户列表页面 -->
<template>
	<div class="mcTableIndex">
		<div class="mcTableList">
			<el-input
				ref="code"
				v-model="code"
				size="small"
				placeholder="点击输入条码"
				style="margin-bottom: 10px; outline: none; color: #da70d6"
				@keyup.native="examineeNameKeyup($event)"
			/>
			<el-table
				:data="tableData"
				border
				style="width: 100%"
				ref="table"
				height="calc(100vh - 242px)"
				@selection-change="handleSelectionChange"
				@row-click="handleClickTableRow"
				:row-style="rowClass"
				:cell-style="{ padding: '0' }"
			>
				<!-- 是否开启选择框 begin -->
				<el-table-column type="selection" width="40"> </el-table-column>
				<!-- 是否开启选择框 end -->
				<!-- 字段列展示 begin -->
				<el-table-column
					:prop="item.prop"
					class="target"
					:label="item.label"
					:width="item.width ? item.width : 'auto'"
					:align="item.align ? item.align : 'left'"
					v-for="(item, index) in tableColumn"
					:key="index"
				>
				</el-table-column>
				<!-- 字段列展示 end -->
			</el-table>
		</div>
		<div class="mcCommonPager" v-if="tableData && tableData.length > 0">
			<el-pagination
				background
				layout="total"
				:total="pageCount"
				:page-size="pageSize"
				:current-page="pageNo"
				@current-change="pageChange"
			>
				>
			</el-pagination>
		</div>
	</div>
</template>

<script>
	import { AjaxCommon } from "@/libs/apiCommon"
	import { PopupCommon } from "@/libs/popupCommon"
	// import { PopupCommon } from "@/libs/popupCommon";
	export default {
		data() {
			return {
				multipleSelection: [],
				code: "",
				state: 0,
				tableData: [],
				tableColumn: [
					{
						prop: "label", // 由examineeCode-examineeName-examineeEn组成
						label: "受试者信息",
					},
				],
				pageSize: 20,
				pageCount: 0,
				pageNo: 1,
			}
		},
		props: {
			/** 接口所需参数:stage */
			stage: {
				type: String,
			},
			businessType: {
				type: String,
			},
		},
		methods: {
			examineeNameKeyup(e) {
				if (e.target.value.length === 10) {
					if (this.state == false) {
						let self = this

						let examineeCode = e.target.value
						let row
						for (let i = 0; i < self.tableData.length; i++) {
							if (self.tableData[i].code == examineeCode) {
								row = self.tableData[i]
							}
						}
						if (row) {
							//this.$refs.table.clearSelection();
							this.$refs.table.toggleRowSelection(row)
							self.$emit("addClick")
						} else {
							PopupCommon.notify("info", "查无此人")
						}

						self.state = true
						setTimeout(() => {
							self.state = false
							self.code = ""
						}, 1000)
					}
				}
			},
			// 点击行勾选数据
			handleClickTableRow(row) {
				this.$refs.table.toggleRowSelection(row)
			},
			// 点击行取消勾选数据
			handleClearTableRow() {
				this.$refs.code.focus()
				this.$refs.table.clearSelection()
			},
			// renderHeader() {
			// 	return (
			// 		<el-input v-model="code" size="small" placeholder="点击输入条码" />
			// 	)
			// },
			rowClass(row) {
				if (row.row.doneStatus) {
					return {
						height: "40px",
						"background-color": "#B0CAE5",
					}
				} else {
					return {
						height: "40px",
					}
				}
			},
			// 页码点击事件,p当前页
			pageChange(p) {
				this.pageNo = p
				this.getData()
			},
			/** 改变选中行的值 */
			handleSelectionChange(val) {
				if (val.length > 0) {
					val = val.map((t) => {
						t.examineeCode = t.code
						return t
					})
				}
				this.multipleSelection = val
				this.$emit("handleSelectionChange", val)
			},
			getData() {
				let self = this
				const stage = this.stage ? this.stage : ""
				const businessType = this.businessType ? this.businessType : ""
				AjaxCommon(
					"/ccdc/common/leftQuery/getExaminee?stage=" +
						stage +
						"&businessType=" +
						businessType,
					"get"
				).then((res) => {
					if (res.data.code == 200) {
						self.tableData = res.data.result
						self.pageCount = res.data.result.length
					}
				})
			},
			refresh() {
				this.tableData = []
				this.pageNo = 1
				this.getData()
			},
		},
		mounted() {
			this.getData()
			this.$refs.code.focus()
		},
	}
</script>

<style scoped>
	.mcTableIndex {
		margin-top: 10px;
		height: calc(100vh - 135px);
		margin-right: 5px;
	}

	.mcTableIndex .mcTableList {
		/* 避免出现滚动条 */
		max-height: calc(100% - 35px);
		overflow: hidden;
	}

	.mcTableIndex .mcCommonPager {
		margin-top: 10px;
		background-color: #fff;
		padding: 5px 0;
	}
</style>
